use sea_orm_migration::prelude::*;

/// User on a homeserver that has username/password credentials.
#[derive(DeriveIden)]
pub enum User {
    Table,
    Id,
    Username,
    PasswordDigest,
    Active,
    CreatedAt,
}

/// Used to authenticate user actions within a session, using a pubkey signing
/// requests.
#[derive(DeriveIden)]
pub enum Session {
    Table,
    Id,
    UserId,
    Pubkey,
    ExpiresAt,
}

/// Any device we've seen, with tracking DIR updates, or an equivocation record.
#[derive(DeriveIden)]
pub enum Device {
    Table,

    /// Device record ID.
    Id,

    /// Device ident hash
    DeviceId,

    /// Sequence number to aid in making atomic update queries.
    Seqno,

    /// Boolean flag set if it's the most recent record for the device.  If
    /// true, then there's SHOULD NOT be any other entries in the database for
    /// this device ID with a greater seqno.
    IsMostRecentKnown,

    /// If the entry is an equivocation.
    IsEquivocation,

    /// Encoded DIR data or equivocation record
    Record,
}

/// Represents the devices that a user owns, possibly with some other metadata.
#[derive(DeriveIden)]
pub enum UserDevice {
    Table,

    /// ID of the entry, in case we ever need to refer succinctly to the
    /// relation itself.
    Id,

    /// Owning user's ID.
    UserId,

    /// Device ID.
    DeviceId,

    /// Timestamp added.
    Added,
}

/// Stupid-simple key-value store for the client to store run-time state in,
/// codec-encoded.
#[derive(DeriveIden)]
pub enum StateProp {
    Table,
    Key,
    Value,
}

/// Basic job queue entries.
#[derive(DeriveIden)]
pub enum PendingJob {
    Table,

    /// Job ID.
    Id,

    /// Time the job was created.
    CreatedAt,

    /// Job queue name.
    QueueName,

    /// Number of times a worker tried to execute the job.
    Tries,

    /// Last attempt, used to eventually control retries.
    LastTry,

    /// Time after which we should retry this job.  Or null when the can be
    /// retried immediately.
    NextTryAfter,

    /// Job data, usually codec-encoded.
    Payload,
}

#[derive(DeriveIden)]
pub enum CompletedJob {
    Table,

    /// Job ID, copied from pending job queue.
    Id,

    /// Time the job was created.
    CreatedAt,

    /// Job queue name.
    QueueName,

    /// When it was completed, either successfully or through some failure.
    CompletedAt,

    /// Length of time it actually took to invoke the job, in milliseconds, if known.
    RunDuration,

    /// Total number of tries before the job was completed, if known.
    TotalTries,

    /// Job status.
    ///
    /// * 0 = ok
    /// * 1 = permanent failure
    /// * 2 = aborted due to excessive retries
    /// * 3 = payload decode error (shouldn't normally occur)
    /// * any other value is some other error and the msg field should be set
    Status,

    /// Any message we might have about the job status to aid in manual
    /// troubleshooting in the case of failures.
    Msg,

    /// Job data, usually codec-encoded.  This might get pruned.
    Payload,
}

/// Authoritative table for message queues, irrespective of their readers.
#[derive(DeriveIden)]
pub enum MessageQueue {
    Table,

    /// Low-level queue ID, used as primary key.
    QueueId,

    /// Name of the table.
    Name,
    // will add garbage collection settings here
}

/// Normalized table for device subscriptions to message queues.
#[derive(DeriveIden)]
pub enum DeviceQueueSub {
    Table,

    /// Queue subscription ID
    Id,

    /// Queue name.
    Name,

    /// Device ID of the subscription.
    DeviceId,

    /// Message queue ID, also primary key of named table.
    QueueId,

    /// Low level reader ID (should be unique to this reader).
    ReaderId,
}
